package accel.test;

import java.util.List;

import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

/**
 * Android accelerometer sensor demo.  Prints sensor XYZ readings to the GUI
 * @author Matthew Zizzi
 */
public class AndroidAccelDemo extends Activity implements SensorEventListener{
	private Sensor accelSensor;
	private TextView xtv;
	private TextView ytv;
	private TextView ztv;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //Get gui objects
        xtv = (TextView)this.findViewById(R.id.xtv);
        ytv = (TextView)this.findViewById(R.id.ytv);
        ztv = (TextView)this.findViewById(R.id.ztv);
        
        //Set up the listener for the accelerometer
        SensorManager sm = (SensorManager)this.getSystemService(SENSOR_SERVICE);
        List<Sensor> list = sm.getSensorList(SensorManager.SENSOR_ACCELEROMETER);
        if (list.size() > 0 && list.get(0) != null) {
        	accelSensor = list.get(0); 
        	sm.registerListener(this, accelSensor, SensorManager.SENSOR_DELAY_GAME);
        	Log.d("accel", accelSensor.getName());
        }
    }

	/**
	 * Event is triggered when a SensorEvent object is sent to this activity
	 * float event.values[] is the array of (X,Y,Z) readings. 
	 */
	@Override
	public void onSensorChanged(SensorEvent event) {
		//Do something useful with (X,Y,Z) readings here.
		//For this demo the textviews are set with the readings
		if (event.values.length == 3) {
			xtv.setText("" + event.values[0]);
			ytv.setText("" + event.values[1]);
			ztv.setText("" + event.values[2]);
		}
	}
	
	/** Not used...  needed when implementing SensorEventListener */
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {}
}